<?php
/**
 * Authenticate check for admin area
 *
 * <code>
 *  $username = "user";
 *  $realm = "desktop";
 *  $password = md5('password');
 *  $passwordForFile = md5("$username:$realm:$password");
 * </code>
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */

class Login_LoginController extends Zend_Controller_Action
{
    /**
     * Initialize the general options
     *
     * @return void
     */
    public function init()
    {
        //$this->_helper->layout->enableLayout();
        $this->_helper->layout->setLayout('layout');
        $this->view
             ->headScript()
             ->appendFile('/login/js/submitform.js');
    }
    /**
     * Login form
     *
     * @return void
     */
    public function indexAction()
    {
       try {
           $this->_setCss('layout');
           $this->_setCss('login');

           $form = new Login_Form_Login;
           $buildForm = $form->build();

           $request = $this->_request->getPost();
       
           if($request) {

               if($buildForm->isValid($request)) {
                   $auth = new Login_Model_Auth;
                   $user = $auth->authenticate($request);

                   if($user === FALSE) {
                       $this->_forward(
                           'app',
                           'error',
                           'error',
                           array(
                               'message' => 'Benutzername oder Passwort falsch!',
                               'back' => array('module' => 'rockitbos')));
                   } else {
                       $auth = Zend_Auth::getInstance();
                       $auth->getStorage()
                            ->write(array('username' => $user['users_name']));
                       
                       $acl = new Login_Model_Acl;
                       $acl->setAcl($user);
                       $this->_forward('index', 'dashboard', 'dashboard');
                   }
               }
           }
           $this->view->form = $buildForm;
       
       } catch (Zend_Exception $e) {

           $this->_forward('app', 'error', 'error', array('log' => $e));
        }

    }
    protected function _setCss($file) {

        $this->view
             ->headLink()
             ->appendStylesheet(
                 DIRECTORY_SEPARATOR . 'login' .
                 DIRECTORY_SEPARATOR . 'css' .
                 DIRECTORY_SEPARATOR . $file . ".css"
               );
    }
}